/**
 * 
 */
Ext.ns('Ext.lams', 'Ext.lams.frame');
Ext.lams.frame.MainingPanel = Ext.extend(Ext.TabPanel, {
	initComponent : function() {
		Ext.lams.frame.MainingPanel.superclass.initComponent.call(this);
		this._cache = {};
		var main = this;
		Ext.Ajax.request({
					url : 'getBtn.action',
                  //  async:false,
					success : function(o) {
                      //  alert(o.responseText);
						var result = Ext.util.JSON.decode(o.responseText);
                        if(result.success)
						  main.btns = result.data;
                        else
                            document.location = Utils.getPath() + "/login.html";
					}
				});

	},
	loadTab : function(_node, succ) {
		var n = this.getComponent(_node.id);
		if (n) {
			this.setActiveTab(n);
		} else {
			var c = {
				'id' : _node.id,
				'title' : _node.text,
				 'btns' : this.btns[0][_node.id],
				closable : true
			};
			var pn;
			this.getEl().unmask();
			if (succ === false) {
				n = this.add(Ext.apply(c, {
							html : '该页面尚未实现!'
						}));
			} else {
				var pn = this.findPanel(_node, _node.clazz);
				// alert(node.id);
				if (pn == 'wait') {
					this.getEl().mask("正在载入JS文件!");
					return;
				};
				n = this.add(new pn(c));
			}
		}
		n.show().doLayout();
		// if (n.ds)
		// n.ds.load(n.initParams);
	},
	capitalize : function(value) {
		return !value ? value : value.charAt(0).toUpperCase() + value.substr(1);
	},
	addPanel : function(_name, _panel) {
		if (!this._cache)
			this._cache = {};
		this._cache[_name] = _panel;
	},
	findPanel : function(_node, _name) {
		var ret = this._cache[_node.id], t = this;
		// var name1 = this.capitalize(_name);
		if (!ret) {
			var pn = _name;
			var ret = eval(pn);
			// this.addPanel(_name,ret);
		}
		if (!ret) {
			var succ = function(o) {
				if (o.responseText) {
					if (window.execScript)
						window.execScript(o.responseText);
					else
						window.eval(o.responseText);
					t._cache[_node.id] = t.loadTab(_node, true);
				}
			};
			var fail = function(o) {
				t.loadTab(_node, false);
			};
			Ext.Ajax.request({
						timeout : 3000,// 毫秒
						url : Utils.getPath() + "/" + _node.url,
						success : succ,
						failure : fail
					});
			ret = "wait";
		}
		return ret;
	}
});

Ext.lams.frame.LeftMenu = Ext.extend(Ext.Panel,{
	constructor:function(config){
		var d = Ext.apply( {// default set
			width : 200,
//			split : true,
//			region : 'west',
			defaults : {
				border : false
			},
			layoutConfig : {
				animate : true
			}
		}, config || {});

		config = Ext.apply(d, {
			layout : 'accordion',
			collapsible : true
		});
		
		Ext.lams.frame.LeftMenu.superclass.constructor.call(this,config);
		for(var i=0;i<this.trees.length;i++){
			this.add({
				title:this.trees[i]["text"],//this.trees[i].getRootNode().text,
				autoScroll:true,
				items:this.initButton(this.trees[i])//[this.trees[i]]
			});
		}
		this.addEvents("nodeClick");
	//	this.initTreeEvent();
	},
	initButton:function(data){
		var btns = [];
		var children = data["children"] || [];
		for(var i=0;i<children.length;i++){
			var item = children[i]
			var btn = new Ext.Button({
				tooltip : item["text"],
				cls:'fc',
				iconCls:item['className'],
				attributes:item,
				handler:this.onNodeClick,
				scope:this
			});
			btns.push(btn);
		}
		return btns;
	},
	initEvents:function(){
		Ext.lams.frame.LeftMenu.superclass.initEvents.call(this);
		//this.initTabSel();
		//this.initTreeEvent();
	},
	initTabSel:function(){
		var th = this, c = this.items;
		for(var i=0;i<this.items.length;i++){
			var p = c.itemAt(i);
			var t = p.items.itemAt(0);
			p.tree = t;
			t.tabPanel = p;
			t.beforeMethod('initEvents', function() {
				 this.getSelectionModel().init = function() {
					 };
				}, t);
			var unevent = new function() {
				this.init = function(tree) {
				}
			};
			t.selModel = unevent;
		}
		this.tabsel = new Ext.lams.TabSel({
			tabBox : this
		});
	},
	initTreeEvent:function(){
		if(!this.items) return;
		for (var i = 0;i < this.items.length; i++) {
			var p = this.items.itemAt(i);
			if (p)
			var t = p.items.itemAt(0);
			t?t.on('click',this.onNodeClick,this):'';
		}
	},
	onNodeClick:function(node,event){
//		if(node && node.isLeaf()){
			event.stopEvent();
			this.fireEvent('nodeClick',node.attributes);
//		}
	}
	
});